/* -+- file  ./kf2/GUI/SubWindow.cs -+- */
/*
 * kf2 jabber client
 * ~~~~~~~~~~~~~~~~~
 *
 * Copyright (C) 2006-2007 Przemysław Sitek
 * 				<przemyslaw.sitek at wp.pl> 
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

using System;
using Habazie.kf2.Core;

namespace Habazie.kf2.GUI
{
	public delegate void RequestHandler (object o, string r);

	///<summary>A window that can be tabbed</summary>
	public class SubWindow : Gtk.EventBox
	{
		private int m_NumEvents;
		private string m_Title = "";
		private string m_ShortTitle = null;
		private Gdk.Pixbuf m_Icon = null;
		
		/// <summary>
		/// 	Title has changed 	
		/// </summary>
		public event EventHandler TitleChanged = null;
		
		/// <summary>
		/// 	Window state has changed (ie. active state) 	
		/// </summary>
		public event EventHandler StateEvent = null;
		
		/// <summary>
		/// 	Window is to be deleted
		/// </summary>
		public event Gtk.DeleteEventHandler DeleteEvent = null;

		
		public event RequestHandler RequestEvent = null;
//		public event EventHandler FocusInEvent = null;
//		public event EventHandler FocusOutEvent = null;
		
		
		public SubWindow() : base ()
		{
//			StateEvent += delegate { Util.Debug ("gui", "StateEvent ({0}, {1})", Title, IsActive); };
		}
		
		
		/// <summary>
		/// 	Number of events for this window 	 	
		/// </summary>
		public int NumEvents {
			set {
				m_NumEvents = value;
				UpdateTitle ();
			}
			get { return m_NumEvents; }
		}
		
		
		/// <summary>
		/// 	Title of this window 	
		/// </summary>
		public string Title {
			get {
				return m_Title;
			}
			set {
				m_Title = value;
				UpdateTitle ();
			}
		}


		/// <summary>
		/// 	Short title for a window
		/// </summary>
		/// <remarks>
		/// 	Short title is displayed on eg tabs
		/// </remarks>
		public string ShortTitle {
			get {
				return m_ShortTitle;
			}
			set {
				m_ShortTitle = value;
				UpdateTitle ();
			}
		}


		/// <summary>
		/// 	Icon for this window 	
		/// </summary>
		public Gdk.Pixbuf Icon {
			get {
				return m_Icon;
			}
			set {
				m_Icon = value;
				UpdateTitle ();
			}
		}
		
		
		/// <summary>
		/// 	Whether this window is active 	
		/// </summary>
		public bool IsActive {
			get {
				TabContainer parent = this.Parent as TabContainer;
				if (parent != null) {
					return parent.IsActiveChild (this);
				} else {
					return false;
				}
			}
		}
		
		
		public void SetDefaultSize (int w, int h)
		{
		}
		
		
		/// <summary>
		/// 	Notify this window that a state event occurred
		/// </summary>
		public void NotifyStateEvent ()
		{
			if (StateEvent != null)
				StateEvent (this, new System.EventArgs ());
		}
		
		
		///	<summary>
		/// 	Whether this window can be detached
		/// </summary>
		public virtual bool IsDetachable ()
		{
			return true;
		}
		
		
		/// <summary>
		/// 	Update title text 	
		/// </summary>
		protected virtual void UpdateTitle ()
		{
//			Util.Debug ("gui", "SubWindow.UpdateTitle ()");
//			if (m_NumEvents == 0)
//				base.Title = m_Title;
//			else
//				base.Title = String.Format ("* {0} ({1})", m_Title, m_NumEvents);
			if (TitleChanged != null)
				TitleChanged (this, new EventArgs ());
		}
		
		
		/// <summary>
		/// 	Delete window
		/// </summary>
		public void Delete ()
		{
			if (DeleteEvent != null) {
				Gtk.DeleteEventArgs args = new Gtk.DeleteEventArgs ();
				args.RetVal = false;
				
//				DeleteEvent (this, args);
				if ((bool) args.RetVal == false)
					Destroy ();
			} else {
				Destroy ();
			}
		}


		/// <summary>
		/// 	Move window to top (above other windows)
		/// </summary>
		public void Raise ()
		{
			SendRequest ("raise");
			// blah, workaround
			Toplevel.GdkWindow.Raise ();
			GrabFocus ();
		}

		private void SendRequest (string r)
		{
			if (RequestEvent != null)
				RequestEvent (this, r);
		}
	}
}
